//
//  AllTopicTableViewController.m
//  FlyGift
//
//  Created by lanou3g on 15/7/15.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "AllTopicTableViewController.h"

#import "AllTopicTableViewCell.h"

#import "AllTopicModel.h"

#define kAllTopicURL @"http://api.liwushuo.com/v1/collections?limit=72&offset=0"

@interface AllTopicTableViewController ()

@property(nonatomic, strong)NSMutableArray *allTopicModelArray;

@end

@implementation AllTopicTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    
    [self allTopicDataRequest];
    
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allTopicModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *allTopicCell = @"allTopicCell";
    
    [tableView registerClass:[AllTopicTableViewCell class] forCellReuseIdentifier:allTopicCell];
    
    AllTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:allTopicCell forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.allTopicModel = self.allTopicModelArray[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (kScreenWidth - kP6width(10)) / 2.18 + kP6Height(8);
}

- (void)allTopicDataRequest
{
    [LORequestManger GET:kAllTopicURL success:^(id response) {
        
        if (response == nil)
        {
            return;
        }
        NSDictionary *dictionary = (NSDictionary *)response;
        
        NSDictionary *dataDic = dictionary[@"data"];
        
        NSArray *array = dataDic[@"collections"];
        
        self.allTopicModelArray = [AllTopicModel jsonToAllTopicModelArray:array];
        
        [self.tableView reloadData];
        
    } failure:nil];
}
@end
